package com.rightsidetech.http.filter;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;



import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
//@Component
//@WebFilter(filterName = "RequestResponseLogFilter", urlPatterns = "/*")
public class LogFilter implements Filter {


    //@Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

/*        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;*/


        //打印请求日志(临时)
        try {
            log.info("----------请求开始22----------");
            log.info("url={}", request.getRequestURL());
            log.info("method={}", request.getMethod());
            log.info("ip={}", request.getHeader("Host"));
            log.info("contentType={}", request.getContentType());
            log.info("headers={}", request.getHeaders("token"));
            //log.info("class-method={}", request.getSignature().getDeclaringTypeName() + "." + pjp.getSignature().getName());
            log.info("----------请求参数----------");
            //Object[] args = pjp.getArgs();
            String contentType = request.getHeader("Content-Type");

            Map<String, String[]> ParameterMap = request.getParameterMap();

            log.info("args={}", JSON.toJSONString(ParameterMap));
            //此处要排除流文件类型,比如上传的文件
/*            if (args != null && args.length > 0 && StringUtil.notNullAndWhitSpace(contentType) && !contentType.startsWith("multipart/form-data")) {
                log.info("args={}", JSON.toJSONString(args));
            }*/
        } catch (Exception e) {
            log.error("参数打印错误={}", e.getMessage());
        }

        filterChain.doFilter(request, response);
        return;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

/*        ServletRequest requestWrapper = null;

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;


        //打印请求日志
        try {
            log.info("----------请求开始----------");
            log.info("url={}", request.getRequestURL());
            log.info("method={}", request.getMethod());
            log.info("ip={}", request.getHeader("Host"));
            log.info("contentType={}", request.getContentType());
            //log.info("headers={}", request.getHeaders("token"));
            //log.info("class-method={}", request.getSignature().getDeclaringTypeName() + "." + pjp.getSignature().getName());
            log.info("----------请求参数----------");
            //Object[] args = pjp.getArgs();
            String contentType = request.getHeader("Content-Type");

            if(request.getMethod().equalsIgnoreCase("POST") && !contentType.startsWith("multipart/form-data")){
                requestWrapper = new BodyReaderHttpServletRequestWrapper(request);

            }
            String param = "";
            if (requestWrapper == null) {
                Map<String, String[]> originRequestMap = request.getParameterMap();
                Map<String,String> requestMap = new HashMap<String,String>();
                for (String key : originRequestMap.keySet()) {
                    String[] values = originRequestMap.get(key);
                    requestMap.put(key,values[0]);
                }
                param = JSON.toJSONString(requestMap);
            } else {
                param  = ((BodyReaderHttpServletRequestWrapper) requestWrapper).getBody();
            }
            log.info("args={}", param);

        } catch (Exception e) {
            log.error("参数打印错误={}", e.getMessage());
        }

        filterChain.doFilter(request, response);*/
        //return;
    }

    @Override
    public void destroy() {

    }
}
